package com.wht.bill.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;
import com.wht.bill.bean.AccountBean;
import com.wht.bill.bean.BillBean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;


public class CacheUtils {
    public static final String APP_CACHE_KEY = "app_cache_key";

    /**
     * 保存银行卡
     */
    public static void saveBank(String bankNo) {
        List<AccountBean> bankList = getBankList();
        AccountBean accountBean = new AccountBean();
        accountBean.accountType = "银行卡";
        accountBean.bankNo = bankNo;
        bankList.add(accountBean);
        //去重
        HashSet<AccountBean> hashSet = new HashSet<>(bankList);
        List<AccountBean> uniqueAccountBeanList = new ArrayList<>(hashSet);
        MMKV kv = MMKV.mmkvWithID(APP_CACHE_KEY);
        kv.encode("bank", new Gson().toJson(uniqueAccountBeanList));
    }

    /**
     * 获取银行卡
     */
    public static List<AccountBean> getBankList() {
        String banStr = "";
        MMKV kv = MMKV.mmkvWithID(APP_CACHE_KEY);
        String accountInfoStr = kv.decodeString("bank");
        if (!TextUtils.isEmpty(accountInfoStr)) {
            banStr = accountInfoStr;
        }
        if (!TextUtils.isEmpty(banStr) && JSON.parseArray(banStr, AccountBean.class) != null) {
            return JSON.parseArray(banStr, AccountBean.class);
        } else {
            return new ArrayList<>();
        }

    }

    /**
     * 获取账号列表
     *
     * @return
     */
    public static List<AccountBean> getAccountList() {
        ArrayList<AccountBean> list = new ArrayList<>();
        AccountBean wechatBean = new AccountBean();
        wechatBean.accountType = "微信";
        list.add(wechatBean);
        AccountBean aliBean = new AccountBean();
        aliBean.accountType = "支付宝";
        list.add(aliBean);
        AccountBean moneyBean = new AccountBean();
        moneyBean.accountType = "现金";
        list.add(moneyBean);

        if (getBankList() != null && !getBankList().isEmpty()) {
            list.addAll(getBankList());
        }
        AccountBean otherBean = new AccountBean();
        otherBean.accountType = "其它";
        list.add(otherBean);
        return list;
    }

    /**
     * 添加账单
     */
    public static void addBill(BillBean pBean) {
        List<BillBean> billList = getBillList();
        billList.add(pBean);
        MMKV kv = MMKV.mmkvWithID(APP_CACHE_KEY);
        kv.encode("bill", new Gson().toJson(billList));
    }

    /**
     * 获取账单
     */
    public static List<BillBean> getBillList() {
        String banStr = "";
        MMKV kv = MMKV.mmkvWithID(APP_CACHE_KEY);
        String billStr = kv.decodeString("bill");
        if (!TextUtils.isEmpty(billStr)) {
            banStr = billStr;
        }
        if (!TextUtils.isEmpty(banStr) && JSON.parseArray(banStr, AccountBean.class) != null) {
            return JSON.parseArray(banStr, BillBean.class);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 保存密码
     *
     * @param pwd
     */
    public static void savePwd(String pwd) {
        MMKV kv = MMKV.mmkvWithID(APP_CACHE_KEY);
        kv.encode("pwd", pwd);
    }

    /**
     * 获取密码
     */
    public static String getPwd() {
        String pwdStr = "";
        MMKV kv = MMKV.mmkvWithID(APP_CACHE_KEY);
        String pwd = kv.decodeString("pwd");
        if (!TextUtils.isEmpty(pwd)) {
            pwdStr = pwd;
        }
        return pwdStr;
    }

}
